<template>
  <div class="home-new" ref="target">
    <HomePanel title="新鲜好物" sub-title="新鲜出炉 品质靠谱">
      <template #right><XtxMore path="/"/></template>
      <!-- 面板内容 -->
      <div class="box">
        <transition name="fade">
          <ul class="goods-list" v-if="goods.length">
            <li v-for="item in goods" :key="item.id">
              <RouterLink :to="`/product/${item.id}`">
                <img :src="item.picture" alt="" />
                <p class="name ellipsis">{{ item.name }}</p>
                <p class="price">&yen;{{ item.price }}</p>
              </RouterLink>
            </li>
          </ul>
        </transition>
        <transition name="fade">
          <HomeSkeleton v-if="goods.length === 0"></HomeSkeleton>
        </transition>
      </div>
    </HomePanel>
  </div>
  <!-- 
    处理懒加载
1:导入第三方方法
2：获取需要监听的dom
3:调用第三方方法实现监听
4:在监听的回调函数在返回值为true(显示时)，调用接口，同时关闭监听

   -->
</template>
<script>
// import { ref } from 'vue'
import HomePanel from './home-panel'
import { homeNew } from '@/api/home'
import HomeSkeleton from './home-skeleton.vue'
// import { useIntersectionObserver } from '@vueuse/core'
import { lazyLoad } from '@/hook'
export default {
  name: 'HomeNew',
  components: { HomePanel, HomeSkeleton },
  setup () {
    // 监听方法抽离 ：页面使用了什么 封装抽离，返回二个东西：target监听的dom,数据：goods
    const { goods, target } = lazyLoad(() => homeNew(4))
    return { goods, target }
  }
}
</script>
<style scoped lang="less">
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding: 12px 30px 0 30px;
      text-align: center;
    }
    .price {
      color: @priceColor;
    }
  }
}
.box {
  position: relative;
  height: 406px;
  overflow: hidden;
}

// .fade-enter-active {
//   transition: all 0.5s;
// }
// .fade-enter-from {
//   opacity: 0;
// }
// .fade-leave-active {
//   transition: all 0.5s;
// }
// .fade-leave-to {
//   opacity: 0;
// }
</style>
